
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Configuration;

namespace KbWorksBlogMigrationtool
{

    public class Logging
    {
        private static int _ALL = 100;
        private static int _INFO = 75;
        private static int _DEBUGTIME = 70;
        private static int _ERROR = 50;
        private static int _OFF = 0;
        AppSettingsReader conf = new AppSettingsReader();
        public static int DEBUGTIME
        {
            get { return _DEBUGTIME; }
        }
        public static int ALL
        {
            get { return _ALL; }
        }
        public static int ERROR
        {
            get { return _ERROR; }
        }
        public static int INFO
        {
            get { return _INFO; }
        }
        public static int OFF
        {
            get { return _OFF; }
        }
        public void append(String message, int level,string wssType)
        {
            int logLevel = _ALL;
            bool fileExist = false;

            String logDirectory = Convert.ToString(conf.GetValue("logdirectory", typeof(string)));
            String strLogLevel = Convert.ToString(conf.GetValue("loglevel", typeof(string)));
            switch (strLogLevel)
            {
                case "ALL":
                    logLevel = _ALL;
                    break;
                case "ERROR":
                    logLevel = _ERROR;
                    break;
                case "DEBUGTIME":
                    logLevel = _DEBUGTIME;
                    break;
                case "INFO":
                    logLevel = _INFO;
                    break;
                default:
                    logLevel = _OFF;
                    break;
            }
            if (logLevel >= level)
            {
                DateTime dt = DateTime.Now;
                String filePath = logDirectory + wssType + dt.ToString("yyyyMMdd") + ".log";
                if (!File.Exists(filePath))
                {
                   
                    FileStream fs = File.Create(filePath);
                    fileExist = false;
                    fs.Close();
                }
                else { fileExist = true; }
                try
                {
                    StreamWriter sw = File.AppendText(filePath);
                    if (fileExist == false)
                    {
                        sw.WriteLine("time;message;level");
                        fileExist = true;
                    }
                    sw.WriteLine(dt.ToString("hh:mm:ss") + ";" + message + ";" + level.ToString());
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message.ToString());
                }
            }
        }
    }
}

